package com.taofuxn.yycreazy.util;

import android.content.Context;
import android.content.res.Resources;
import android.util.TypedValue;

/**
 * Created by lol on 2015/12/emphasis_customer.
 */
public class MeasureUtils {
    private static int mScreenWidth;
    private static int mScreenHeight;
    private static int mApplicationHeight;
    private static int statusHeight = -1;
    public static final int lineSize = (int) (getScreenWidth(null) * 0.003f);

    /**
     * 获取屏幕宽度
     * @param context
     * @return
     */
    public static int getScreenWidth(Context context){
        if(mScreenWidth == 0){
            Resources r = getResources(context);
            mScreenWidth = r.getDisplayMetrics().widthPixels;
        }
        return mScreenWidth;
    }

    /**
     * 获取屏幕高度
     * @param context
     * @return
     */
    public static int getScreenHeight(Context context){
        if(mScreenHeight == 0){
            Resources r = getResources(context);
            mScreenHeight = r.getDisplayMetrics().heightPixels;
        }
        return mScreenHeight;
    }

    /**
     * 获取应用程序高度，就是屏幕高度减去状态栏高度
     * @param context
     * @return
     */
    public static int getApplicationHeight(Context context){
        if(mApplicationHeight == 0){
            if(statusHeight <= 0){
                getStatusHeight(context);
            }
            mApplicationHeight = getScreenHeight(context) - statusHeight;
        }
        return mApplicationHeight;
    }

    /**
     * 获取状态栏高度
     * @param context
     * @return
     */
    public static int getStatusHeight(Context context){
        if(statusHeight == -1){
            Resources r = getResources(context);
            try {
                Class<?> clazz = Class.forName("com.android.internal.R$dimen");
                Object object = clazz.newInstance();
                int height = Integer.parseInt(clazz.getField("status_bar_height").get(object).toString());
                statusHeight = r.getDimensionPixelOffset(height);
                if(statusHeight <= 0){
                    int resouresId = r.getIdentifier("status_bar_height", "dimen", "android");
                    if(resouresId > 0){
                        statusHeight = r.getDimensionPixelOffset(resouresId);
                    }
                }
              /*  if(statusHeight <= 0){
                    Rect rectangle= new Rect();
                    Window window= context.getWindow();
                    window.getDecorView().getWindowVisibleDisplayFrame(rectangle);
                    int statusBarHeight= rectangle.top;
                }*/
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        return statusHeight;
    }

    /**
     * pix转换为sp
     * @param context
     * @return
     */
    public static int pixToSp(int value,Context context){
        int result = 0;
        Resources r = getResources(context);
        float scaledDensity = r.getDisplayMetrics().scaledDensity;
        result = (int) (value / scaledDensity + 0.5f);
        return result;
    }

    /**
     * pix转换为sp
     * @param context
     * @return
     */
    public static float pixToSp(float value,Context context){
        Resources r = getResources(context);
        float scaledDensity = r.getDisplayMetrics().scaledDensity;
        float result = value / scaledDensity;
        return result;
    }

    /**
     * sp转换为pix
     * @param context
     * @return
     */
    public static int spToPix(int value,Context context){
        Resources r = getResources(context);
        int result = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_SP,value,
                r.getDisplayMetrics());
        return result;
    }

    /**
     * pix转换为dip
     * @param context
     * @return
     */
    public static int pixToDip(int value,Context context){
        int result = 0;
        Resources r = getResources(context);
        float densityDpi = r.getDisplayMetrics().densityDpi;
        result = (int) (value / densityDpi + 0.5f);
        return result;
    }

    /**
     * dip转换为pix
     * @param context
     * @return
     */
    public static int dipToPix(int value,Context context){
        Resources r = getResources(context);
        int result = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP,value,
                r.getDisplayMetrics());
        return result;
    }

    /**
     * 获取资源Resources
     * @param context
     * @return
     */
    private static Resources getResources(Context context){
        Resources resources;
        if (context == null){
            resources = Resources.getSystem();
        }else{
            resources = context.getResources();
        }
        return resources;
    }

}
